﻿using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.MiniApp;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.DtoModel.Wx;
using VisionCloud.Domain.Filters;
using VisionSurface.Service.Ms;

namespace VisionSurface.Web.Controllers.Ms
{
    /// <summary>
    /// 任务管理
    /// </summary>
    [ApiVersion("1.0")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiExplorerSettings(GroupName = "任务管理")]
    [Route("api/miniapp/ms/[controller]/[Action]")]
    public class TaskController : ApiBaseController
    {
        public TaskManageService taskManageService { get; set; }

        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result", typeof(TaskInfoResponseDto))]
        public async ValueTask<ApiResult> GetList(QueryTaskRequestDto dto)
        {
            var list = await taskManageService.GetTaskListAsync(dto);

            return new ApiResult { Code = AjaxCode.Success, Result = list };
        }

        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为任务详情", typeof(MsTask))]
        public async ValueTask<ApiResult> Add(TaskFm model)
        {
            var result = await taskManageService.AddTask(model);

            return new ApiResult { Code = AjaxCode.Success, Result = result };
        }

        /// <summary>
        /// 复制任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为任务详情", typeof(MsTask))]
        public async ValueTask<ApiResult> Copy(TaskFm model)
        {
            if (model.TaskId == Guid.Empty)
                return new ApiResult(AjaxCode.ParamError, "参数错误");

            model.IsCopyTask = true;
            var result = await taskManageService.AddTask(model);

            return new ApiResult { Code = AjaxCode.Success, Result = result };
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为任务详情", typeof(MsTask))]
        public async ValueTask<ApiResult> Update(TaskFm model)
        {
            var result = await taskManageService.UpdateTask(model);

            return new ApiResult { Code = AjaxCode.Success, Result = result };
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="model"></param>
        /// <remarks>
        /// Code = 0，表示删除成功
        /// </remarks>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        public async ValueTask<ApiResult> Delete(TaskIdsDto model)
        {
            if (model?.TaskIds.Count == 0)
                return new ApiResult(AjaxCode.ParamError, "参数错误");

            await taskManageService.DeleteTasks(model.TaskIds);

            return new ApiResult { Code = AjaxCode.Success };
        }

        /// <summary>
        /// 任务详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为任务详情", typeof(TaskSfm))]
        public async ValueTask<ApiResult> Detail(MsTaskIdDto model)
        {
            var result = await taskManageService.GetTaskDetail(model.TaskId);

            return new ApiResult { Code = AjaxCode.Success, Result = result };
        }

        /// <summary>
        /// 任务绑定消息模板
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        public async ValueTask<ApiResult> BindTemplate(WxMessageAddDto model)
        {
            return await taskManageService.BindTaskMessageTemplate(model.TaskIds, model.TemplateId, TokenModel.WxOpenId);
        }

        /// <summary>
        /// 新增任务点云的上传批次
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        public async ValueTask<ApiResult> AddUploadBatch(TaskDotCloudBatchRequestDto model)
        {
            return await taskManageService.AddTaskUploadBatchAsync(model);
        }

        /// <summary>
        /// 更新任务状态：只允许更新为已开始和已完成
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        public async ValueTask<ApiResult> UpdateStatus(UpdateTaskStatusRequestDto dto)
        {
            return await taskManageService.UpdateTaskStatusAsync(dto);
        }

        /// <summary>
        /// 下发任务
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为是否成功", typeof(bool))]
        public async Task<ApiResult> IssueTask(MsTaskIdDto dto)
        {
            var res = await taskManageService.IssueTask(dto.TaskId);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 完成站点采集
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为是否成功", typeof(bool))]
        public async Task<ApiResult> CollectTaskPoint(CollectTaskPointDto dto)
        {
            var res = await taskManageService.CollectTaskPoint(dto);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 保存采集作业编号
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为是否成功", typeof(bool))]
        public async Task<ApiResult> SaveCollectJobId(CollectJobDto dto)
        {
            var res = await taskManageService.SaveCollectJobId(dto);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 获取采集作业编号
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为站点(带状态)集合", typeof(List<CollectAndUpload>))]
        public async Task<ApiResult> GetCollectJobId(TaskPointIndexDto dto)
        {
            var res = await taskManageService.GetCollectJobId(dto);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 获取任务下需要上传点云的站点
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为站点(带状态)集合", typeof(List<CollectAndUpload>))]
        public async Task<ApiResult> GetNeedUploadPoint(MsTaskIdDto dto)
        {
            var res = await taskManageService.GetNeedUploadPoint(dto.TaskId);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 重置站点状态为开始采集
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为是否成功", typeof(bool))]
        public async Task<ApiResult> ResetPointStatus(ResetTaskPointDto dto)
        {
            var res = await taskManageService.ResetPointStatus(dto);
            return new ApiResult { Code = AjaxCode.Success, Result = res };
        }

        /// <summary>
        /// 设置站点采集分辨率
        /// </summary>
        [HttpPost, SwaggerOperation(Tags = new[] { "任务管理" })]
        [SwaggerResponse(0, "Result为是否成功", typeof(bool))]
        public async Task<ApiResult> SetTaskPointResolution(TaskPointResolutionDto dto)
        {
            var res = await taskManageService.SetTaskPointResolution(dto);
            return ApiResult.Success().SetData(res);
        }
    }
}